import { marked } from 'marked';
import DOMPurify from 'dompurify';

// 配置marked
marked.setOptions({
    breaks: true,    // 自动转换换行符为<br>
    gfm: true        // 支持GitHub风格的Markdown
});

export const parseMarkdown = (rawText) => {
    // 转换Markdown为HTML
    const htmlContent = marked.parse(rawText);

    // 净化HTML并保留换行
    return DOMPurify.sanitize(htmlContent, {
        ALLOWED_TAGS: [
            'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
            'p', 'br', 'strong', 'em', 'ul', 'ol', 'li',
            'hr', 'pre', 'code', 'span', 'div'
        ]
    });
};    